package com.txzt.gaea.business.project.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.base.service.DataPermissionService;
import com.txzt.gaea.business.project.domain.model.ProjectInfo;
import com.txzt.gaea.business.project.domain.vo.ProjectInfoVo;
import com.txzt.gaea.business.project.service.IProjectInfoService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/project/info")
public class ProjectInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectInfoController.class);

    @Autowired
    private IProjectInfoService projectInfoService;
    @Autowired
    private DataPermissionService dataPermissionService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param projectInfo
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectInfo projectInfo) {
        logger.info(projectInfo.toString());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        List<Long> roleIds = Arrays.stream(adminUserVO.getRoleid().split(",")).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
        projectInfo.setRoleIds(roleIds);
        dataPermissionService.getCurrentUserPermissionType(projectInfo);
        PageQuery<ProjectInfo> pageList = projectInfoService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectInfo));
        return RtnInfo.success(this.translate(pageList));
    }

    /**
     * 分页查询(流程查询)
     * @param pageNumber
     * @param pageSize
     * @param projectInfo
     * @return
     */
    @GetMapping(value = "/listCheck")
    public RtnInfo<?> listCheck(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectInfo projectInfo) {
        logger.info(projectInfo.toString());
        dataPermissionService.getCurrentUserPermissionType(projectInfo);
        PageQuery<ProjectInfo> pageList = projectInfoService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectInfo));
        return RtnInfo.success(this.translate(pageList));
    }

    private PageQuery<ProjectInfo> translate(PageQuery<ProjectInfo> pageList){
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译更新人
            item.setUpdateName(TranslateUtil.getUserName(item.getUpdateBy()));
            //翻译项目组长
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门（离大谱）
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return pageList;
    }

    /**
     * 计划统计
     * @return
     */
    @GetMapping(value = "/statistics")
    public RtnInfo<?> statistics() {
        List<Map> mapList = projectInfoService.statisticsProjectInfo();
        mapList.forEach(map ->{
            if (null != map.get("departId")) {
                map.put("departName",TranslateUtil.getDepartmentName(Long.valueOf(map.get("departId").toString())));
            }

        });
        return RtnInfo.success(mapList);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(id);
        if (null == projectInfo) {
            return RtnInfo.SERVER_ERROR;
        }else {
            return RtnInfo.success(projectInfo);
        }

    }

    /**
     * 编辑
     * @param projectInfo
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody ProjectInfo projectInfo) {
        int count = projectInfoService.updateById(projectInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 开题申请
     * @param projectInfoVo
     * @return
     */
    @PutMapping(value = "/open")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> open(@RequestBody ProjectInfoVo projectInfoVo) {
        int count = projectInfoService.openProjectInfo(projectInfoVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 延期申请
     * @param projectInfoVo
     * @return
     */
    @PutMapping(value = "/delay")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delay(@RequestBody ProjectInfoVo projectInfoVo) {
        int count = projectInfoService.delayProjectInfo(projectInfoVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 中止申请
     * @param projectInfoVo
     * @return
     */
    @PutMapping(value = "/stop")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> stop(@RequestBody ProjectInfoVo projectInfoVo) {
        int count = projectInfoService.stopProjectInfo(projectInfoVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }



    /**
     * 结题申请
     * @param projectInfoVo
     * @return
     */
    @PutMapping(value = "/finish")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finish(@RequestBody ProjectInfoVo projectInfoVo) {
        int count = projectInfoService.finishProjectInfo(projectInfoVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


}
